<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25
 * Time: 17:35
 */
namespace app\index\controller;

use think\Controller;
use think\Request;

class TalkLogin extends Controller{
    /**
     *
     * talk login and have three roles
     */
    public function login(Request $request){
        $serial = $request->post('serial');
        $password = $request->post('password');
        $nick = $request->post('nick');
        $role = $request->post('role');
        if($serial == ''){
            return json(array('state' => 0, 'msg' => '房间号不能为空'));
        }
        if($password == ''){
            return json(array('state' => 0, 'msg' => '密码不能为空'));
        }
        if($nick == ''){
            return json(array('state' => 0, 'msg' => '昵称不能为空'));
        }
        $data                 = array();
        $ts = time();
        $data['domain'] = config('zhibo.domain');
        $data['serial'] = $serial;
        $data['username']     = $nick;
        switch($request->post('role')){
            case '主讲(老师)':
                $data['usertype'] = 0;
                break;
            case '助教':
                $data['usertype'] = 1;
                break;
            case '学员':
                $data['usertype'] = 2;
                break;
            case '直播用户':
                $data['usertype'] = 3;
                break;
            case '巡检员':
                $data['usertype'] = 4;
                break;
            default:
                break;
        }
        $data['ts']     = $ts;
        $data['auth'] = md5(config('zhibo.authkey').$ts.$serial.$data['usertype']);
        $data['userpassword'] = hexPass($password);
        $param = '';
        foreach ($data as $k => $v){
            $param .= $k.'='.$v.'&';
        }
        $url = "http://global.talk-cloud.net/WebAPI/entry?".$param;
        return json(array('state' => 1, 'msg' => '成功','url'=>rtrim($url,'&')));
    }
}